﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
public partial class System_warehs_Warehs_list1 : System.Web.UI.Page
{
  
    DataSet ds = new DataSet();
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    int iCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
       

        LoginLogic.IsLogin();


        if (!IsPostBack)
        {
            gv_Bind();
        }
        if (this.hidden.Value != "") //保证添加修改页面回来后，刷新最新数据
        {
            gv_Bind();
        }
        this.hidden.Value = "";
       
    }
    

    /// <summary>
    /// 仓库列表
    /// </summary>
    protected void gv_Bind()
    {
        //strSQL = "SELECT WareHsCode,WareHsName,WareHsType=case WareHsType when 'FSSC' then '公共' else '独立' end FROM TB_Warehs	";
        strSQL = "SELECT WareHsCode,WareHsName,WareHsType FROM TB_Warehs	";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ds = dbHelper.ExecuteDataSet(cmd);
        gvWareH.DataSource = ds;
        gvWareH.DataBind();
    }

    protected void ibtnQuery_Click(object sender, ImageClickEventArgs e)
    {
       ds= QueryWareHDataSet(txt_WareHsCode.Text.Trim(), txt_WareHsName.Text.Trim());
       gvWareH.DataSource = ds;
       gvWareH.DataBind();
    }

    /// 根据条件模糊查询仓库信息
    /// </summary
   
    /// <returns>DataSet</returns>
    /// writer:[谢刚伟]
    /// Create Date:[2015-1-30]

    public DataSet QueryWareHDataSet(string vWareHNo, string vWareHName)
    {
        try
        {
            strSQL = @"SELECT WareHsCode
                                  ,WareHsName
                                  ,WareHsType
                                 
                              FROM 
                                    TB_WareHs
                            WHERE 
                                    (WareHsCode like @vWareHsCode) 
                                and  (WareHsName like @vWareHsName)";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
            dbHelper.AddInParameter(cmd, "@vWareHsCode", DbType.String, "%" + vWareHNo + "%");
            dbHelper.AddInParameter(cmd, "@vWareHsName", DbType.String, "%" + vWareHName + "%");

            ds = dbHelper.ExecuteDataSet(cmd);

        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }

        return ds;
    }


    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txt_WareHsCode.Text = "";
        txt_WareHsName.Text = "";
    }
    protected void ibtnDel_Click(object sender, ImageClickEventArgs e)
    {
        CheckBox cbx = null;
        StringBuilder listId = new StringBuilder();
        for (int i = 0; i < gvWareH.Rows.Count; i++)
        {
            cbx = (CheckBox)gvWareH.Rows[i].Cells[0].FindControl("chkSelect");
            if (cbx.Checked)
            {
                listId.Append("'" + gvWareH.DataKeys[i].Value.ToString() + "',");
            }
        }

        if (listId.Length > 0)
        {
            try
            {
                iCount = DeleteWareHs(listId.ToString().TrimEnd(','));
                if (iCount > 0)
                {
                    //ShowBox.MessageBox(this.Page, "删除成功!"); 如果选择禁止显示对话框，删除功能就会异常
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }

    ///<summary>
    ///删除多个用户
    ///</summary>
    ///<param name="listGroupNo">id</param>
    ///<returns>返回int,0失败,大于等于1成功</returnss>
    /// writer:[Kitty]
    /// Create Date:[2010-5-28]
    ///</summary>
    public int DeleteWareHs(string listId)
    {
        int i = 0;
        try
        {
            
            strSQL = "delete from TB_WareHs where WareHsCode in  (" + listId + ") ";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
            i = dbHelper.ExecuteNonQuery(cmd);
        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }
        return i;
    }

    /// <summary>
    /// 绑定gv触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvWareH_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbtn_WareHsCode = (LinkButton)e.Row.FindControl("lbtn_WareHsCode");

            string str = string.Format("javascript:document.getElementById('{3}').value=window.open('WarehListAdd.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&Mode={0}&WareHsCode={1}&id={2}', window, 'Height=160px,Width=600px,top=250, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
           "Update", lbtn_WareHsCode.CommandArgument.ToString(), gvWareH.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);
            lbtn_WareHsCode.Attributes.Add("onclick", str);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string msg = "shchucehnggong";
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "xx", "<script>alert('" + msg + "');</script>");
    }
}
